/*//////////////////////////////////////////////////////////////////////////////////////////////////////
    FileName:          Main.c
    Author:            Tony Tan
    Version :          V1.0
    Date:              3/31 2011
    Description:  
                       A RF demo program
                       After burnned to two RF development boards, the connection between them is established,
                       When you pressed any key of one board, the LED status of another board is changed at the same time,
                       Once the error ocurrs, the RED LED on the board you pressed key is flushed.

    Version:      
    Function List: 
    History: 
                       Tony Tan    2011/03/31     1.0     First version  
//////////////////////////////////////////////////////////////////////////////////////////////////////*/

#include "types.h"
#include "bsp.h"
#include "main.H"
#include "bk2421.h"
#include "keyboard.h"
#include "spi.h"
#include <avr/pgmspace.h>

volatile UINT8 g_RFIRQValid = FALSE;
volatile UINT8 g_DelayTick = 0;

#define     RFPKT_LEN       5
volatile UINT8 g_RFRecvBuff[RFPKT_LEN];
volatile UINT8 g_RFSendBuff[RFPKT_LEN];

/*****************************************************************************************
Function:       void SystemInit( void )
Parameter: 
                None
Return: 
                None 
Description:
	            System Initialization 
*****************************************************************************************/
void SystemInit( void )
{
    BSPInit();      //hardware board initialize

    BK2421_Initialize();    //RF Initailize

    memset( (UINT8 *)&g_Key, 0, sizeof(KEYPKT) );

}

/*****************************************************************************************
Function:       int main( void )
Parameter: 
                None
Return: 
                None 
Description:
	            
*****************************************************************************************/
int main( void )
{

    SystemInit();

    YLED_OFF();
    GLED_OFF();

    while(1)
    {

#ifdef CARRIER
        RFReceiveProcess();                     //Check RF receive
#endif

#ifdef CARRIER

		CarrierDetect( 0x20 );					//Detect carrier

#else
        KeyBoardScan();                         //Scan board

#endif
        if( g_Key.keychanged )                  //Key Changed?
        {
			g_Key.keychanged = FALSE;
            if( g_Key.keyvalue )                //Any key pressed down?
            {
                RFSendPacket( g_Key.keyvalue ); //Send the Key value
            }
        }
    }
}


/*****************************************************************************************
Function:       void Timer0ISR(void)
Parameter: 
                None
Return: 
                None 
Description:
                Timer0 interrupt service function
                Timer0 overflow period: 1ms
*****************************************************************************************/
void Timer0ISR(void)
{
    g_Key.tick ++;
    if( g_Key.tick >= KEY_SCAN_PERIOD )   
    {
        g_Key.tick = 0;
        g_Key.scanvalid = TRUE;
    }

    g_DelayTick++;
}


/*****************************************************************************************
Function:       void AppExtInt0ISR( void )
Parameter: 
                None
Return: 
                None 
Description:
                INT0 interrupt service function
 
                The interrupt occur when RF IRQ is set to low 
*****************************************************************************************/
void AppExtInt0ISR( void )
{
    g_RFIRQValid = TRUE;
}


/*****************************************************************************************
Function:       void RFReceiveProcess( void )
Parameter: 
                None
Return: 
                None 
Description:
                Check RF receive flag,
                If receive flag is set, then read out the received data from RF RX fifo
                then process the read out data
*****************************************************************************************/
void RFReceiveProcess( void )
{
    UINT8 sta;
    UINT8 rlen;

    if( g_RFIRQValid )
    {
        g_RFIRQValid = FALSE;

        sta = RF_GET_STATUS();      //Get the RF status

        if( sta & STATUS_RX_DR )    //Receive OK?
        {
            //Readout the received data from RX FIFO
            rlen = RF_ReadRxPayload( (UINT8 *)&g_RFRecvBuff, RFPKT_LEN );

            if ( rlen == RFPKT_LEN )
            {
                //Process data
                switch( g_RFRecvBuff[0] )
                {
                case SW_1:
//                  GLED_FLASH();
                    break;
                case SW_2:
//                  GLED_FLASH();
                    break;
                case SW_3:
//                  GLED_FLASH();
                    break;
                case SW_4:
//                  GLED_FLASH();
                   break;
                case SW_5:
//                  GLED_FLASH();
                    break;
                }
            }
        }
        if( sta & STATUS_MAX_RT )   //Send fail?
        {
            RF_FLUSH_TX();          //Flush the TX FIFO 
        }

        RF_CLR_IRQ( sta );          //Clear the IRQ flag
    }
}


/*****************************************************************************************
Function:       void RFSendPacket( UINT8 Key )
Parameter: 
                None
Return: 
                None 
Description:
                Switch the RF to TX mode
                Then write the data to TX fifo, and send out
                After that, a process for check the RF send result added
                Once the RF send over, send fail or send time out, switch the RF to RX mode
*****************************************************************************************/
void RFSendPacket( UINT8 Key )
{
    UINT8 sta;
    UINT16 temp = 100;

    g_RFSendBuff[0] = Key;

    SwitchToTxMode();                                               //Set RF to TX mode

    CLR_CE();
    SPI_Write_Buf(WR_TX_PLOAD, (UINT8 *)&g_RFSendBuff, RFPKT_LEN); // Writes data to TX FIFO
    g_RFIRQValid = FALSE;
    SET_CE();
    while( temp-- );                                               //Wait for Time > 10us
    CLR_CE();

    //Wait for send over
    g_DelayTick = 0;

    while( 1 )
    {
	    if( g_RFIRQValid )
		{
            sta = SPI_Read_Reg( STATUS );   // read register STATUS's value
    
            if( (sta & STATUS_TX_DS) || (sta & STATUS_MAX_RT) )    //TX IRQ
            {
                if( sta & STATUS_MAX_RT )   //if send fail
                {
                    RF_FLUSH_TX();
                }  
    
                RF_CLR_IRQ( sta );          // clear RX_DR or TX_DS or MAX_RT interrupt flag

                GLED_FLASH();
				g_RFIRQValid = FALSE;
                break;
            }
        }
        else if( g_DelayTick >= 5 )         //if timeout
        {
            RF_FLUSH_TX();
            break;
        }
    }

    SwitchToRxMode();    
}





